package com.ibm.cloud.envcheck.kit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.ibm.cloud.envcheck.bean.ActionResult;
import com.ibm.cloud.envcheck.bean.PropertyBean;

public class PropertyService {
	
	
	public static ActionResult checkValue(Properties p,String key,String expectValue){
		if(key==null){
			return new ActionResult();
		}
		String value = p.getProperty(key, null);
		
		if(wrapNullString(value).equals(wrapNullString(expectValue))){
			ActionResult ar = new ActionResult();
			ar.setActualValue(value);
			ar.setExpectValue(expectValue);
			ar.setSuccess(true);
			ar.setKey(key);
			return ar;
		}else{
			ActionResult ar = new ActionResult();
			ar.setExpectValue(expectValue);
			ar.setActualValue(wrapNullString(value));
			ar.setKey(key);
			ar.setMessage("");
			return ar;
		}
		
	}
	
	public static List<ActionResult> checkValues(File file,List<PropertyBean>list){
		Properties prop = new Properties();   
	    FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			prop.load(fis);
			return checkValues(prop,list);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	    return null;
	}
	
	public static List<ActionResult> checkValues(Properties property,List<PropertyBean>list){
		List<ActionResult>resultList = new ArrayList<ActionResult>();
		if(list==null||list.isEmpty()){
			return null;
		}
		for(PropertyBean p:list){
			ActionResult ar = checkValue(property,p.getKey(),p.getExpectValue());
			resultList.add(ar);
		}
		
		return resultList;
	}
	
	private static String wrapNullString(String str){
		if(str==null||str.trim().isEmpty()){
			return "";
		}else{
			return str;
		}
	}
}
